home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / PMPSRC11.ZIP / DQUEUE.C < prev    next >
Text File  |  1991-07-30  |  3KB  |  134 lines

  1. /*
  2.     dqueue.c -- Data queue manipulation
  3.  
  4.   Poor Man's Packet (PMP)
  5.   Copyright (c) 1991 by Andrew C. Payne    All Rights Reserved.
  6.  
  7.   Permission to use, copy, modify, and distribute this software and its
  8.   documentation without fee for NON-COMMERCIAL AMATEUR RADIO USE ONLY is hereby
  9.   granted, provided that the above copyright notice appear in all copies.
  10.   The author makes no representations about the suitability of this software
  11.   for any purpose.  It is provided "as is" without express or implied warranty.
  12.  
  13.     Andrew C. Payne
  14.     12/03/89
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #include <mem.h>
  21. #include "pmp.h"
  22.  
  23. /* DQInit(*dq)
  24.     Initializes a data queue.
  25. */
  26. void DQInit(DQUEUE *dq)
  27. {
  28.     dq->next = NULL;
  29.     dq->len = 0;
  30. }
  31.  
  32. /* DQAdd(*dq, data, len)
  33.     Adds data to a data queue.  Returns TRUE if error.
  34. */
  35. int DQAdd(DQUEUE *dq, byte *d, int len)
  36. {
  37.     struct dqentry    *p;
  38.  
  39. /* don't add zero length items */
  40.     if(len == 0)
  41.         return FALSE;
  42.  
  43. /* allocate a data queue entry */
  44.     p = malloc(sizeof(struct dqentry) + len);
  45.     if(p == NULL)
  46.         return TRUE;
  47.  
  48. /* initialize entry */
  49.     p->next = dq->next;
  50.     p->len = len;
  51.     memcpy(p->data,d,len);        /* copy in data */
  52.  
  53. /* add entry to queue */
  54.     dq->next = p;
  55.     dq->len += len;
  56.     return FALSE;
  57. }
  58.  
  59. /* DQFirst(*dq)
  60.     Returns a pointer to the first queue entry.
  61. */
  62. struct dqentry *DQFirst(DQUEUE *dq)
  63. {
  64.     struct    dqentry    *p;
  65.  
  66.     p = dq->next;
  67.     if(p == NULL)        /* empty */
  68.         return NULL;
  69.  
  70.     while(p->next != NULL)    /* find last */
  71.         p = p->next;
  72.  
  73.     return p;
  74. }
  75.  
  76. /* DQRemoveFirst(*dq)
  77.     Removes the first entry from the data queue.
  78. */
  79. void DQRemoveFirst(DQUEUE *dq)
  80. {
  81.     struct    dqentry    *p,*q;
  82.  
  83.     p = dq->next;
  84.     if(p == NULL)        /* empty queue */
  85.         return;        /* nothing read */
  86.  
  87. /* find the last entry in the queue */
  88.     q = dq;
  89.     while(p->next != NULL) {
  90.         q = p;
  91.         p = p->next;
  92.     }
  93.  
  94. /* remove the entry */
  95.     dq->len -= p->len;    /* maintain size count */
  96.     free(p);
  97.     q->next = NULL;
  98. }
  99.  
  100. /* DQExtract(dq, dest, len)
  101.     Extracts the specified number of bytes from the DQUEUE into the
  102.     destination buffer specified.
  103.  
  104.     Will extract at most the number of bytes in the buffer.
  105. */
  106. void DQExtract(DQUEUE *dq, byte *dest, int len)
  107. {
  108.     struct dqentry    *p;
  109.  
  110.     len = min(len, dq->len);
  111.  
  112.     while(len) {
  113.         p = DQFirst(dq);
  114.         if(p->len > len) {        /* use partial */
  115.             memcpy(dest,p->data,len);
  116.             memcpy(p->data, p->data+len, p->len-len);
  117.             p->len -= len;
  118.             dq->len -= len;
  119.             break;
  120.         } else {            /* use whole */
  121.             memcpy(dest,p->data,p->len);
  122.             len -= p->len;
  123.             dest += p->len;
  124.             DQRemoveFirst(dq);
  125.         }
  126.     }
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.